package com.example.transaction;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.ServiceApi;
import org.mengyun.tcctransaction.api.Compensable;
import org.mengyun.tcctransaction.dubbo.context.DubboTransactionContextEditor;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName ServiceApiImpl
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/8  12:36
 * @Version 1.0
 */
//要把这个方法注册成为bean 这个方式是spring的  注册成功service的要使用dubbo方式
@Component()
@Service(interfaceClass = ServiceApi.class) // 对外暴露服务
public class TransationServiceApiImpl implements ServiceApi {


    @Override
    @Compensable(confirmMethod = "confirmSendMsg", cancelMethod = "cancelSendMsg", transactionContextEditor = DubboTransactionContextEditor.class)
    public String sendMsg(String message) {
        System.out.println("TransationServiceApiImpl - sendMsg try message --> " + message);

        if (message.equals("123")) {
            throw new RuntimeException(" 你看看中招了吧 ！！！ ");
        }

        return "provider message --> " + " : " + message;
    }

    @Override
    @Compensable(confirmMethod = "confirmIsTrueSeats", cancelMethod = "cancelIsTrueSeats", transactionContextEditor = DubboTransactionContextEditor.class)
    public boolean isTrueSeats(String seats) throws Exception {
        if (seats.equals("1,2,3")) {
            throw new IllegalAccessException("");
        } else {
            return true;
        }
    }


    public boolean confirmIsTrueSeats(String seats) throws Exception {
        System.out.println("this is confirmIsTrueSeats ");
        return true;
    }


    public boolean cancelIsTrueSeats(String seats) throws Exception {
        System.out.println("this is cancelIsTrueSeats ");
        return true;
    }


    @Override
    @Compensable(confirmMethod = "confirmIsNotSold", cancelMethod = "cancelIsNotSold", transactionContextEditor = DubboTransactionContextEditor.class)
    public boolean isNotSold(String seats) throws Exception {
        if (seats.equals("4,5")) {
            throw new IllegalAccessException("");
        }
        return true;
    }

    public boolean confirmIsNotSold(String seats) throws Exception {
        System.out.println("this is confirmIsNotSold ");
        return true;
    }

    public boolean cancelIsNotSold(String seats) throws Exception {
        System.out.println("this is cancelIsNotSold ");
        return true;
    }

    /**
     * 要注意订单幂等性问题
     *
     * @param fildId
     * @param seats
     * @param seatNum
     * @return
     * @throws Exception
     */
    @Override
    @Compensable(confirmMethod = "confirmSaveOrder", cancelMethod = "cancelSaveOrder", transactionContextEditor = DubboTransactionContextEditor.class)
    public String saveOrder(String fildId, String seats, String seatNum) throws Exception {
        System.out.println("创建一个待支付状态的订单");

        return "";
    }

    public String confirmSaveOrder(String fildId, String seats, String seatNum) throws Exception {
        System.out.println("将订单状态修改为支付中状态");
        return "";
    }

    public String cancelSaveOrder(String fildId, String seats, String seatNum) throws Exception {
        System.out.println("将订单状态修改为已关闭状态");
        return "";
    }


    public String confirmSendMsg(String message) {

        System.out.println("TransationServiceApiImpl - confirmSendMsg  message --> " + message);
        return "provider message --> " + " : " + message;
    }


    public String cancelSendMsg(String message) {
        System.out.println("TransationServiceApiImpl - cancelSendMsg  message --> " + message);
        return "provider message --> " + " : " + message;
    }
}
